package com.ruoyi.web.web.controller;

import com.ruoyi.web.core.config.AppConf;
import com.ruoyi.accountant.domain.AccountDetail;
import com.ruoyi.accountant.service.AccountDetailService;
import com.ruoyi.web.web.form.AccountDetailForm;
import com.ruoyi.web.web.form.Validation;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/accountDetail")
public class AccountDetailController {

    @Autowired
    private AccountDetailService accountDetailService;

    @PostMapping("/export")
    public void export(AccountDetailForm form, HttpServletResponse response) throws IOException {

        Validation validate = form.validate();
        if(validate.getOk()) {

            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;fileName=account.xls");


            Page<AccountDetail> allByForm = (Page<AccountDetail>) accountDetailService.findByForm(form);
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet();
            HSSFRow row = sheet.createRow(0);

            row.createCell(0, CellType.STRING).setCellValue("交易类型");
            row.createCell(1, CellType.STRING).setCellValue("收付日期");
            row.createCell(2, CellType.STRING).setCellValue("凭证日期");
            row.createCell(3, CellType.STRING).setCellValue("提出金额");
            row.createCell(4, CellType.STRING).setCellValue("存入金额");
            row.createCell(5, CellType.STRING).setCellValue("账户余额");
            row.createCell(6, CellType.STRING).setCellValue("附注说明");
            row.createCell(7, CellType.STRING).setCellValue("收入原因");
            row.createCell(8, CellType.STRING).setCellValue("对象");
            row.createCell(9, CellType.STRING).setCellValue("标签");


            int rowNum = 1;
            for (AccountDetail detail : allByForm.getContent()) {
                HSSFRow row1 = sheet.createRow(rowNum++);

                row1.createCell(0, CellType.STRING).setCellValue(detail.getDtype());
                row1.createCell(1, CellType.STRING).setCellValue(AppConf.DATE_FORMAT.format(detail.getTransactionDate()));
                row1.createCell(2, CellType.STRING).setCellValue(detail.getProofDate() != null ? AppConf.DATE_FORMAT.format(detail.getProofDate()) : "");
                row1.createCell(3, CellType.STRING).setCellValue(detail.getOutmoney().toString());
                row1.createCell(4, CellType.STRING).setCellValue(detail.getInmoney().toString());
                row1.createCell(5, CellType.STRING).setCellValue(detail.getBalance().toString());
                row1.createCell(6, CellType.STRING).setCellValue(detail.getComment());
                row1.createCell(7, CellType.STRING).setCellValue(detail.getOfReason() != null ? detail.getOfReasonObject().getRname() : "");
                row1.createCell(8, CellType.STRING).setCellValue(detail.getObject());
                row1.createCell(9, CellType.STRING).setCellValue(detail.getTag());
            }

            workbook.write(response.getOutputStream());
            workbook.close();
        }
    }
}
